package com.cs.framework.jdbc.mybatis3.handler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

/**
 * Boolean类型转换：true - Y、false - N
 * 
 * @author hedongzhou
 * 
 */
public class BooleanTypeHandler implements TypeHandler<Boolean> {

	public Boolean getResult(ResultSet rs, String param) throws SQLException {
		return getBoolean(rs.getString(param));
	}

	public Boolean getResult(ResultSet rs, int param) throws SQLException {
		return getBoolean(rs.getString(param));
	}

	public Boolean getResult(CallableStatement cs, int param) throws SQLException {
		return getBoolean(cs.getString(param));
	}

	public void setParameter(PreparedStatement ps, int param, Boolean value, JdbcType type) throws SQLException {
		ps.setString(param, value != null && value == true ? "Y" : "N");
	}

	private Boolean getBoolean(String value) {
		return value != null && value.equalsIgnoreCase("Y") ? true : false;
	}

}
